/*
 * -------------------------------------------------------------------------- *
 * This is a Practice Work from the 'Algorithms, Part-I' course form Coursera.
 * 
 * -------------------------------------------------------------------------- *
 * Date:	29-Aug-2013 8:41:08 am
 */
package com.gs.coursera.algorithms.core.uf;

/**
 * @author Sabuj Das | sabuj.das@gmail.com
 *
 */
public class QuickUnion extends UnionFind{

	public QuickUnion(int n) {
		super(n);
	}

	protected int root(int i){
		while(i != getNodes()[i]){
			i = getNodes()[i];
		}
		return i;
	}
	
	@Override
	public void union(int p, int q) {
		int i = root(p);
		int j = root(q);
		getNodes()[i] = j;
	}

	@Override
	public boolean isConnected(int p, int q) {
		return root(p) == root(q);
	}

	
}
